package com.ui;

import java.awt.EventQueue;
import java.io.File;
import java.sql.Connection;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.JTable;

import com.controlador.Conexion;
import com.controlador.JavaExcelWriter;
import com.controlador.JavaPDFWriter;
import com.controlador.Operaciones;
import com.util.Constantes;

import javax.swing.JButton;
import javax.swing.ImageIcon;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class ReporteDiario extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTable tablaDatos;
	Object[][] dataObj = null;
	DefaultTableModel modelo = null;
	
	Conexion connex = new Conexion();
	Connection conn = null;
	Operaciones op = new Operaciones();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ReporteDiario frame = new ReporteDiario();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ReporteDiario() {
		setTitle("Daily Report");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 700, 400);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		
		JLabel lblReporteDiarioEntradassalidas = new JLabel("I/O Daily Report");
		
		JScrollPane scrollPane = new JScrollPane();
		
		JButton btnNewButton = new JButton("");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String[] encabezado = {"ID","Serial #","Model","Form Type","Process","Reuse","Engineer","User","Date"}; 
				Vector<?> m = modelo.getDataVector();
				new JavaPDFWriter().escribePDF(encabezado,m);
				
				//mandar abrir el archivo
				try 
				{ 
					String rutaAbsoluta = new File(Constantes.ARCHIVO_PDF_TEMPORAL).getAbsolutePath();
					@SuppressWarnings("unused")
					Process p = Runtime.getRuntime().exec ("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ rutaAbsoluta ); 
					
				} 
				catch (Exception e) 
				{ 
					System.err.println("Error: "+e.getMessage()); 
				}
			}
		});
		btnNewButton.setIcon(new ImageIcon(ReporteDiario.class.getResource("/com/util/img/pdf-ico.png")));
		
		JButton btnNewButton_1 = new JButton("");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				String[] encabezado = {"ID","Serial #","Model","Form Type","Process","Reuse","Engineer","User","Date"};
				Vector<?> m = modelo.getDataVector();
				new JavaExcelWriter().escribeExcel(m,encabezado);
				
				//mandar abrir el archivo
				try 
				{ 
					String rutaAbsoluta = new File(Constantes.ARCHIVO_EXCEL_TEMPORAL).getAbsolutePath();
					@SuppressWarnings("unused")
					Process p = Runtime.getRuntime().exec ("rundll32 SHELL32.DLL,ShellExec_RunDLL "+ rutaAbsoluta ); 
					
				} 
				catch (Exception e) 
				{ 
					System.err.println("Error: "+e.getMessage()); 
				}
				
			}
		});
		btnNewButton_1.setIcon(new ImageIcon(ReporteDiario.class.getResource("/com/util/img/excel-ico.png")));
		GroupLayout gl_contentPane = new GroupLayout(contentPane);
		gl_contentPane.setHorizontalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addContainerGap()
					.addComponent(lblReporteDiarioEntradassalidas)
					.addPreferredGap(ComponentPlacement.RELATED, 305, Short.MAX_VALUE)
					.addComponent(btnNewButton)
					.addPreferredGap(ComponentPlacement.RELATED)
					.addComponent(btnNewButton_1)
					.addGap(23))
				.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 674, Short.MAX_VALUE)
		);
		gl_contentPane.setVerticalGroup(
			gl_contentPane.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_contentPane.createSequentialGroup()
					.addGroup(gl_contentPane.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_contentPane.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_contentPane.createParallelGroup(Alignment.BASELINE)
								.addComponent(lblReporteDiarioEntradassalidas)
								.addComponent(btnNewButton))
							.addPreferredGap(ComponentPlacement.RELATED))
						.addGroup(Alignment.TRAILING, gl_contentPane.createSequentialGroup()
							.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
							.addComponent(btnNewButton_1)
							.addGap(21)))
					.addComponent(scrollPane, GroupLayout.DEFAULT_SIZE, 27, Short.MAX_VALUE))
		);
		
		conn=connex.AccederBD();
		
		String fr = new Registro().getFechaFormateada();
				fr="fecha='"+fr+"'";
		
		dataObj = op.select("Equipos", "id,noSerie,modelo,tipo,proceso,reuso,idIngeniero,usuario,fecha", fr,"proceso", conn);
		String[] encabezado = {"ID","Serial #","Model","Form Type","Process","Reuse","Engineer","User","Date"}; 
		modelo = new DefaultTableModel(dataObj,encabezado);
		tablaDatos = new JTable();
		tablaDatos.setModel(modelo);
		scrollPane.setViewportView(tablaDatos);
		contentPane.setLayout(gl_contentPane);
	}
}
